package com.it.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.domain.Inventory;
import com.it.domain.ResponseResult;
import com.it.service.IInventoryService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Arrays;

/**
 * 库存管理Controller
 */
@RestController
@RequestMapping("/component/inventory")
public class InventoryController {

    @Autowired
    private IInventoryService inventoryService;

    /**
     * 分页查询库存列表
     */
    @GetMapping("/list")
    public ResponseResult<IPage<Inventory>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long inventoryId,
            @RequestParam(required = false) Long productId,
            @RequestParam(required = false) Integer quantity,
            @RequestParam(required = false) String location) {

        // 构建分页对象
        Page<Inventory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Inventory> queryWrapper = new LambdaQueryWrapper<>();

        // 构建查询条件
        if (inventoryId != null) {
            queryWrapper.eq(Inventory::getInventoryId, inventoryId);
        }
        if (productId != null) {
            queryWrapper.eq(Inventory::getProductId, productId);
        }
        if (quantity != null) {
            queryWrapper.eq(Inventory::getQuantity, quantity);
        }
        if (location != null && !location.isEmpty()) {
            queryWrapper.like(Inventory::getLocation, location);
        }

        queryWrapper.orderByDesc(Inventory::getInventoryId);
        IPage<Inventory> result = inventoryService.page(page, queryWrapper);

        return ResponseResult.success(result);
    }

    /**
     * 获取库存详细信息
     */
    @GetMapping("/{inventoryId}")
    public ResponseResult<Inventory> getInfo(@PathVariable Long inventoryId) {
        Inventory inventory = inventoryService.getById(inventoryId);
        return inventory != null ?
                ResponseResult.success(inventory) :
                ResponseResult.error("库存记录不存在");
    }

    /**
     * 新增库存
     */
    @PostMapping
    public ResponseResult<String> add(@RequestBody Inventory inventory) {
        // 参数验证
        if (inventory.getProductId() == null) {
            return ResponseResult.error("产品ID不能为空");
        }
        if (inventory.getQuantity() == null || inventory.getQuantity() <= 0) {
            return ResponseResult.error("库存数量必须大于0");
        }
        if (inventory.getLocation() == null || inventory.getLocation().trim().isEmpty()) {
            return ResponseResult.error("库存位置不能为空");
        }

        boolean result = inventoryService.save(inventory);
        return result ?
                ResponseResult.success("新增成功") :
                ResponseResult.error("新增失败");
    }

    /**
     * 修改库存
     */
    @PutMapping
    public ResponseResult<String> edit(@RequestBody Inventory inventory) {
        // 参数验证
        if (inventory.getInventoryId() == null) {
            return ResponseResult.error("库存ID不能为空");
        }
        if (inventory.getProductId() == null) {
            return ResponseResult.error("产品ID不能为空");
        }
        if (inventory.getQuantity() == null || inventory.getQuantity() <= 0) {
            return ResponseResult.error("库存数量必须大于0");
        }
        if (inventory.getLocation() == null || inventory.getLocation().trim().isEmpty()) {
            return ResponseResult.error("库存位置不能为空");
        }

        boolean result = inventoryService.updateById(inventory);
        return result ?
                ResponseResult.success("修改成功") :
                ResponseResult.error("修改失败");
    }

    /**
     * 删除库存
     */
    @DeleteMapping("/{inventoryIds}")
    public ResponseResult<String> remove(@PathVariable Long[] inventoryIds) {
        if (inventoryIds == null || inventoryIds.length == 0) {
            return ResponseResult.error("请选择要删除的记录");
        }

        boolean result = inventoryService.removeByIds(Arrays.asList(inventoryIds));
        return result ?
                ResponseResult.success("删除成功") :
                ResponseResult.error("删除失败");
    }
}